<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Response;

class ApiBaseRequest extends FormRequest
{
    /**
     * 重写验证响应
     * @param Validator $validator
     * @throws \Illuminate\Validation\ValidationException
     */
    public function failedValidation(Validator $validator)
    {
        $errors = [];
        $oriErrors = $validator->getMessageBag()->toArray();
        foreach ($oriErrors as $err){
            $errors[] = implode('、', $err);
        }
        header('Content-Type: text/javascript; charset=utf8');
        $response = json_encode([
            'status' => 'error',
            'data' => $oriErrors,
            'title' => '数据错误',
            'message' => implode('、', $errors),
        ]);
        exit($response);
    }
}
